/*
 *  
 **/

valyJs = function(){
    this.throwException = true
    
    this.returnHandler = function(request){
        if(request.expression())
            return true;
        else{
            if(this.throwException){
                throw new ValyJsError(
                request.errorMessage,
                request.caller);
            }
            else{
                return false;
            }
        }
    }
}

valyJs.prototype.about = {
    version : "<%= @version %>",
    name : "ValyJs",
    homepage : "http://code.google.com/p/valjjs/",
    developer : "Dejan FAJFAR"
}
        
valyJs.prototype.setThrowsException = function(throwExceptions){
    throwException = throwExceptions;
}
        
valyJs.prototype.executeIfExists = function(request){
    try{
        this.exists(request.elementId);
        this.exists(request.func);
    }
    catch(err){
        throw new ValyJsError(
        "executeIfExists called without 'elementId' or 'func' parameter",
        arguments.callee.caller.toString());
    }
            
    var element = document.getElementById(request.elementId)
    if(element)
        request.func(element);
    else
        throw new ValyJsError(
    "Element with Id: " + request.elementId + " not found",
    arguments.callee.caller.toString());
}
        
valyJs.prototype.exists = function(item){
    return this.returnHandler({
        expression : function(){return item != undefined},
        caller : arguments.callee.caller.toString(),
        errorMessage : "valyJs.exists function called on a non existing item"
    });
}

function ValyJsError(message,caller){
this.name = "ValyJsError",
this.message = message,
this.caller = caller
}
ValyJsError.prototype = Error.prototype;
ValyJsError.prototype.toString = function(){
return "MESSAGE: " + this.message + " CALLER: " + this.caller;
}

window.valy = new valyJs();